package cn.community.system.web.controller;

import cn.community.system.query.*;
import cn.community.system.service.IAdminReportService;
import cn.community.system.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dataAdminReport")
public class AdminReportController {

    @Autowired
    private IAdminReportService adminReportService;

    @RequestMapping("/activityReportList")
    @RequiresPermissions(value = {"dataAdminReport/activityReportList","超管-活动信息统计列表"},logical = Logical.OR)
    // 统计所有活动的个数和活动总时长（根据时间段、分组类型【年/月/日/社团】搜索统计）
    public String list(Model model, @ModelAttribute("qo") AdminReportQuery qo){
        model.addAttribute("page",adminReportService.selectAllActivityReport(qo));
        return "dataReport/admin/activityReportList";
    }

    @RequestMapping("/deviceLendReportList")
    @RequiresPermissions(value = {"dataAdminReport/deviceLendReportList","超管-设备借用统计列表"},logical = Logical.OR)
    // 统计设备借出/归还的个数和次数（根据时间段、分组类型【年/月/日/社团】搜索统计）
    public String deviceReportList(Model model, @ModelAttribute("qo") AdminDeviceReportQuery qo){
        model.addAttribute("page",adminReportService.selectAllDeviceReport(qo));
        return "dataReport/admin/deviceLendReportList";
    }

    @RequestMapping("/deviceReturnReportList")
    @RequiresPermissions(value = {"dataAdminReport/deviceReturnReportList","超管-设备归还统计列表"},logical = Logical.OR)
    // 统计设备借出/归还的个数和次数（根据时间段、分组类型【年/月/日/社团】搜索统计）
    public String deviceReturnReportList(Model model, @ModelAttribute("qo") AdminDeviceReportQuery qo){
        model.addAttribute("page",adminReportService.selectReturnDeviceReport(qo));
        return "dataReport/admin/deviceReturnReportList";
    }

    @RequestMapping("/activityReportListByBar") // 活动柱状图
    @RequiresPermissions(value = {"dataAdminReport/activityReportListByBar","超管-活动统计柱状图"},logical = Logical.OR)
    public String listByBar(Model model, @ModelAttribute("qo") AdminReportQuery qo){
        //获取分页结果集
        //PageInfo pageInfo = customerReportService.selectCustomerReport(qo);
        //List<Map> list = pageInfo.getList();
        //获取所有数据(不分页)
        List<Map> list = adminReportService.listAll(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储y轴的两个数据
        ArrayList yList1 = new ArrayList();
        ArrayList yList2 = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            yList1.add(map.get("activityNumbers"));
            yList2.add(map.get("activityTimes"));
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        System.out.println(JSON.toJSONString(xList));
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList1",JSON.toJSONString(yList1));
        model.addAttribute("yList2",JSON.toJSONString(yList2));
        model.addAttribute("groupTypeName", MessageUtil.changAdminActivityMsg(qo));
        return "dataReport/activityListByBar";
    }

    @RequestMapping("/activityReportListByPie")  // 活动个数饼图
    @RequiresPermissions(value = {"dataAdminReport/activityReportListByPie","超管-活动个数饼图"},logical = Logical.OR)
    public String listByPie(Model model, @ModelAttribute("qo") AdminReportQuery qo){
        //获取所有数据(不分页)
        List<Map> list = adminReportService.listAll(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储饼图的数据
        ArrayList yList = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            HashMap<String, Object> temp = new HashMap<>();
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("activityNumbers"));
            yList.add(temp);
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changAdminActivityMsg(qo));//分组类型
        return "dataReport/activityListByPie";
    }

    @RequestMapping("/activityReportListByPie2")  // 活动总时长饼图
    @RequiresPermissions(value = {"dataAdminReport/activityReportListByPie2","超管-活动时长饼图"},logical = Logical.OR)
    public String listByPie2(Model model, @ModelAttribute("qo") AdminReportQuery qo){
        //获取所有数据(不分页)
        List<Map> list = adminReportService.listAll(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储饼图的数据
        ArrayList yList = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            HashMap<String, Object> temp = new HashMap<>();
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("activityTimes"));
            yList.add(temp);
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changAdminActivityMsg(qo));//分组类型
        return "dataReport/activityListByPie2";
    }



    @RequestMapping("/deviceReportListByBar") // 设备借用柱状图
    @RequiresPermissions(value = {"dataAdminReport/deviceReportListByBar","超管-设备借用柱状图"},logical = Logical.OR)
    public String deviceListByBar(Model model, @ModelAttribute("qo") AdminDeviceReportQuery qo){
        //获取分页结果集
        //PageInfo pageInfo = customerReportService.selectCustomerReport(qo);
        //List<Map> list = pageInfo.getList();
        //获取所有数据(不分页)
        List<Map> list = adminReportService.listDeviceAll(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储y轴的两个数据
        ArrayList yList1 = new ArrayList();
        ArrayList yList2 = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            yList1.add(map.get("deviceCount"));
            yList2.add(map.get("deviceNumbers"));
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        System.out.println(JSON.toJSONString(xList));
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList1",JSON.toJSONString(yList1));
        model.addAttribute("yList2",JSON.toJSONString(yList2));
        model.addAttribute("groupTypeName", MessageUtil.changAdminDeviceMsg(qo));
        return "dataReport/deviceListByBar";
    }

    @RequestMapping("/deviceReportListByPie")  // 设备借用次数饼图
    @RequiresPermissions(value = {"dataAdminReport/deviceReportListByPie","超管-设备借用次数饼图"},logical = Logical.OR)
    public String deviceListByPie(Model model, @ModelAttribute("qo") AdminDeviceReportQuery qo){
        //获取所有数据(不分页)
        List<Map> list = adminReportService.listDeviceAll(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储饼图的数据
        ArrayList yList = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            HashMap<String, Object> temp = new HashMap<>();
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("deviceCount"));
            yList.add(temp);
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changAdminDeviceMsg(qo));//分组类型
        return "dataReport/deviceListByPie";
    }

    @RequestMapping("/deviceReportListByPie2")  // 设备借用个数饼图
    @RequiresPermissions(value = {"dataAdminReport/deviceReportListByPie2","超管-设备借用个数饼图"},logical = Logical.OR)
    public String deviceListByPie2(Model model, @ModelAttribute("qo") AdminDeviceReportQuery qo){
        //获取所有数据(不分页)
        List<Map> list = adminReportService.listDeviceAll(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储饼图的数据
        ArrayList yList = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            HashMap<String, Object> temp = new HashMap<>();
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("deviceNumbers"));
            yList.add(temp);
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changAdminDeviceMsg(qo));//分组类型
        return "dataReport/deviceListByPie2";
    }



    @RequestMapping("/deviceReturnReportListByBar") // 设备归还柱状图
    @RequiresPermissions(value = {"dataAdminReport/deviceReturnReportListByBar","超管-设备归还柱状图"},logical = Logical.OR)
    public String deviceReturnListByBar(Model model, @ModelAttribute("qo") AdminDeviceReportQuery qo){
        //获取分页结果集
        //PageInfo pageInfo = customerReportService.selectCustomerReport(qo);
        //List<Map> list = pageInfo.getList();
        //获取所有数据(不分页)
        List<Map> list = adminReportService.listDeviceReturn(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储y轴的两个数据
        ArrayList yList1 = new ArrayList();
        ArrayList yList2 = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            yList1.add(map.get("deviceCount"));
            yList2.add(map.get("deviceNumbers"));
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        System.out.println(JSON.toJSONString(xList));
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList1",JSON.toJSONString(yList1));
        model.addAttribute("yList2",JSON.toJSONString(yList2));
        model.addAttribute("groupTypeName", MessageUtil.changAdminDeviceMsg2(qo));
        return "dataReport/deviceListByBar2";
    }

    @RequestMapping("/deviceReturnReportListByPie")  // 设备借用次数饼图
    @RequiresPermissions(value = {"dataAdminReport/deviceReturnReportListByPie","超管-设备借用次数饼图"},logical = Logical.OR)
    public String deviceReturnListByPie(Model model, @ModelAttribute("qo") AdminDeviceReportQuery qo){
        //获取所有数据(不分页)
        List<Map> list = adminReportService.listDeviceReturn(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储饼图的数据
        ArrayList yList = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            HashMap<String, Object> temp = new HashMap<>();
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("deviceCount"));
            yList.add(temp);
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changAdminDeviceMsg2(qo));//分组类型
        return "dataReport/deviceReturnListByPie";
    }

    @RequestMapping("/deviceReturnReportListByPie2")  // 设备借用个数饼图
    @RequiresPermissions(value = {"dataAdminReport/deviceReturnReportListByPie2","超管-设备借用个数饼图"},logical = Logical.OR)
    public String deviceReturnListByPie2(Model model, @ModelAttribute("qo") AdminDeviceReportQuery qo){
        //获取所有数据(不分页)
        List<Map> list = adminReportService.listDeviceReturn(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储饼图的数据
        ArrayList yList = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            HashMap<String, Object> temp = new HashMap<>();
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("deviceNumbers"));
            yList.add(temp);
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changAdminDeviceMsg2(qo));//分组类型
        return "dataReport/deviceReturnListByPie2";
    }
}